/*
 * @Author: suxunying
 * @desc: shared模块
 * @Date: 2021-07-21 11:28:32
 * @LastEditors: suxunying
 * @LastEditTime: 2021-07-26 13:53:50
 * @FilePath: \bmp-poc\micro\src\shared\index.ts
 */
import { Shared } from './shared';

class SharedModule {
    static shared = new Shared();

    static listener: Array<any> = [];

    /**
     * 重载 shared
     */
    static overloadShared(shared: any) {
        console.log('init', shared);
        SharedModule.shared = shared;
        shared.init(() => {
            SharedModule.listener.forEach((fn) => {
                fn();
            });
        });
    }

    static subscribe(fn: any) {
        if (!fn) throw Error('缺少参数');
        if (fn.length) {
            SharedModule.listener.push(...fn);
        } else {
            SharedModule.listener.push(fn);
        }
    }

    /**
     * 获取 shared 实例
     */
    static getShared() {
        return SharedModule.shared;
    }
}

export default SharedModule;
